<template>
  <div>
    <el-button type="primary" style="margin-top:20px" @click="add" size="mini">农药购买登记</el-button>
    <el-button type="primary" style="margin-top:20px" size="mini" @click="importInfo()">导入</el-button>
    <el-button type="primary" style="margin-top:20px" size="mini" @click="downLoadExcel()">导出</el-button>
    <SearchBar :searchList="searchList" @search="search"></SearchBar>
    <el-table :data="tableData"  border style="width: 100%">
      <el-table-column prop="pesticidesSerialNum" label="购买序号"  align="center"> </el-table-column>
      <el-table-column prop="pesticidesNum" label="农药编号" align="center"> </el-table-column>
      <el-table-column prop="pesticidesName" label="农药名称" align="center"> </el-table-column>
      <el-table-column prop="pesticidesType" label="所属类别" align="center" :formatter="(row,column)=>dictChange(row,column,'pesticidesType')"> </el-table-column>
      <el-table-column prop="unit" label="计量单位" align="center"  :formatter="(row,column)=>dictChange(row,column,'unit')"> </el-table-column>
      <el-table-column prop="pesticidesQuantity" label="购买数量" align="center"> </el-table-column>
      <el-table-column prop="pesticidesUnitPrice" label="购买单价" align="center"> </el-table-column>
      <el-table-column prop="pesticidesTotalPrice" label="购买总额" align="center"> </el-table-column>
      <el-table-column prop="pesticidesDate" label="购买日期" align="center"> </el-table-column>
      <el-table-column prop="pesticidesAddress" label="购买地点" align="center"> </el-table-column>
      <el-table-column prop="remark" label="备注" align="center"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle  @click="updateData(row)"></el-button>
           <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteData(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pagination.pageNum"
      :page-sizes="[5,10, 20, 30, 40]"
      :page-size="pagination.pageSize"
      layout=" prev, pager, next,sizes"
      :total="pagination.total"
      align='center'>
    </el-pagination>
    <Add ref="add" :type="type"></Add>

    <el-dialog title="导入" :visible.sync="dialogFormVisible">
      <CubeUpload
        :tpl-url="tplUrl"
        :action-url="actionUrl"
        @close="uploadVisible = false"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 组件
import Add from './add.vue'
import CubeUpload from "@/components/CubeUpload"
//工具

export default {
  name: "pesticiedePurchase",
  components: {
    Add,
    CubeUpload
  },
 data() {
    return {
      searchList:[
        {placeholder:"园地名称",type:"input", key:'gardenName'},
        {placeholder:"种植品种",type:"option", key:'gardenVar',options:[{label:"苹果", value:"apple"},{label:"香蕉",value:"banana"}]},
        {placeholder1:"起始日期",placeholder2:"截止日期",type:"date",key:'date'},
      ],
      pagination:{
        pageNum:1,
        pageSize:10,
        total:100
      },
      dialogFormVisible:false,
      tplUrl: '/car/downloadImportTemplate',
      actionUrl: '/car/importCar',
      tableData: [],
      type:"",
      searchObj:{}
    }
  },
  created(){
    this.updatePageList()
  },
  methods:{
    //点击查询按钮
    async search(formInline){
      this.searchObj=formInline
      let res=await this.$API.pesticiedePur.getPageList({pageNum:this.pagination.pageNum,pageSize:this.pagination.pageSize,...this.searchObj})
      if(res.code==200){
        this.tableData=res.data.rows
        this.pagination.total=res.data.rowCount
      }
    },
    //更新分页器
    async updatePageList(page=1){
      this.pagination.pageNum=page
      // this.pagination.pageSize=size
      let res=await this.$API.pesticiedePur.getPageList({pageNum:this.pagination.pageNum,pageSize:this.pagination.pageSize,...this.searchObj})
      if(res.code==200){
        this.tableData=res.data.rows
        this.pagination.total=res.data.rowCount
      }
    },
    //修改数据
    updateData(row){
      this.type="update"
      this.$refs.add.dialogFormVisible=true
      this.$refs.add.form={...row}
      // console.log(row)
      // this.$API.pesticiedePur.saveOrUpdate(row)
    },
    //删除数据
    async deleteData(row){
      try{
        let res=await this.$API.pesticiedePur.deleteData({id:row.pesticidesId})
        if(res.code==200){
          this.$message({type:"success",message:"删除成功"})
          this.updatePageList()
        }
      }catch{
          this.$message({type:"error",message:"删除失败"})
      }
    },
    //添加园地信息
    add(){
      this.type="add"
      this.$refs.add.dialogFormVisible=true
      this.$refs.add.form= {
        pesticidesSerialNum: '',
        pesticidesNum: '',
        pesticidesName: '',
        pesticidesType: '',
        delivery: '',
        unit:'',
        pesticidesQuantity:'',
        pesticidesUnitPrice: '',
        pesticidesTotalPrice: '',
        pesticidesDate: '',
        pesticidesAddress: '',
        remark: '',
      }
    },
    //监听每页展示页数的改变
    handleSizeChange(size) {
      this.pagination.pageSize=size
      this.updatePageList(this.pagination.pageNum)
      // console.log(`每页 ${size} 条`);
    },
    //监听当前页的改变
    handleCurrentChange(page) {
      this.updatePageList(page)
      // console.log(`当前页: ${page}`);
    },
    //导出按钮
    downLoadExcel(){
      // let data = {  }   //可以传入参数
      // downLoadExcel(data).then((res) => {
      //   let ex = exportData(res)
      // })
       this.$message.success('正在导出请稍候')
      const prams = this.$refs['CubeTableRender'] && this.$refs['CubeTableRender'].getParam()
      exportExcelList(prams).then(res => {
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        const downloadElement = document.createElement('a')
        downloadElement.href = href
        downloadElement.download = decodeURI(res.headers['filename'])
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      }).catch(err => {
        this.$message({
          message: '下载失败！',
          type: 'error',
          showClose: true
        })
      })
    },
    //导入按钮
    importInfo(){
      // this.CubeDialogTitle = '导入车辆信息'
      this.dialogFormVisible = true
      // this.tplUrl = '/car/downloadImportTemplate'
      // this.actionUrl = '/car/importCar'
      return
    }
  }
};
</script>

<style scoped>
</style>